package maiyatian.videolive.live.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import maiyatian.videolive.IActivity;
import maiyatian.videolive.R;
import maiyatian.videolive.bean.LiveBean;
import maiyatian.videolive.live.LiveBoxActivity;
import maiyatian.videolive.live.adapter.LookCommentAdapter;
import maiyatian.videolive.live.adapter.LookListAdapter;
import maiyatian.videolive.live.fragment.OverLiveLiverFragment;

/**
 * 作者：王中阳 on 2016/5/15 14:17
 * 邮箱：wangzhongyang@maiyatian.com
 * QQ(微信):425772719
 * 公司：北京麦芽田网络科技有限公司
 */
@ContentView(R.layout.activity_enjoy)
public class LiveEnjoyActivity extends IActivity implements View.OnClickListener, LookListAdapter.IClickListener {
    public static final String TAG = "LiveEnjoyActivity";
    //围观列表
    @ViewInject(R.id.rvLook)
    RecyclerView rvLook;
    List<LiveBean> datas;
    LookListAdapter lookListAdapter;
    //评论列表
    @ViewInject(R.id.rvLookComment)
    RecyclerView rvLookComment;
    LookCommentAdapter lookCommentAdapter;
    //底部工具栏
    View includeBottom;
    ImageView talk, message, share, gift;
    Button btnSend;
    LinearLayout boxInput;
    RelativeLayout boxBottom;
    @ViewInject(R.id.ivOver)
    ImageView ivOver;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        ivOver.setOnClickListener(this);
        initViews();
    }

    private void initViews() {
        //围观列表
        rvLook.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        datas = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            datas.add(new LiveBean());
        }
        lookListAdapter = new LookListAdapter(this, this);
        rvLook.setAdapter(lookListAdapter);
        lookListAdapter.setMdatas(datas);
        //评论列表
        rvLookComment.setLayoutManager(new LinearLayoutManager(this));
        lookCommentAdapter = new LookCommentAdapter(this);
        rvLookComment.setAdapter(lookCommentAdapter);
        lookCommentAdapter.setMdatas(datas);
        //底部工具栏
        includeBottom = findViewById(R.id.includeBottom);
        boxBottom = (RelativeLayout) includeBottom.findViewById(R.id.boxBottom);
        boxInput = (LinearLayout) includeBottom.findViewById(R.id.boxInput);
        //输入框
        btnSend = (Button) includeBottom.findViewById(R.id.btnSend);
        btnSend.setOnClickListener(this);
        talk = (ImageView) includeBottom.findViewById(R.id.talk);
        talk.setOnClickListener(this);
        message = (ImageView) includeBottom.findViewById(R.id.message);
        message.setOnClickListener(this);
        share = (ImageView) includeBottom.findViewById(R.id.share);
        share.setOnClickListener(this);
        gift = (ImageView) includeBottom.findViewById(R.id.gift);
        gift.setOnClickListener(this);

        // TODO: 2016/5/17  弹出键盘输入框 及隐藏
//        keyboardShowHide();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivOver:
                Intent intent = new Intent();
                intent.setClass(this, LiveBoxActivity.class);
                intent.putExtra(IActivity.PAGETO, OverLiveLiverFragment.TAG);
                startActivity(intent);
                break;
            case R.id.talk:
                boxBottom.setVisibility(View.GONE);
                boxInput.setVisibility(View.VISIBLE);
                break;
            case R.id.message:

                break;
            case R.id.share:

                break;
            case R.id.gift:

                break;
            case R.id.btnSend: //发送
                hideInput();
                break;
        }
    }

    private void showInput() {
        boxInput.setVisibility(View.VISIBLE);
        boxBottom.setVisibility(View.GONE);
    }

    private void hideInput() {
        boxBottom.setVisibility(View.VISIBLE);
        boxInput.setVisibility(View.GONE);
    }

    //围观人列表
    @Override
    public void onItemClicked(int position) {

    }


    //监听键盘显示及隐藏
    private void keyboardShowHide() {
        includeBottom.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Log.v(TAG, "detailMainRL.getRootView().getHeight() = " + includeBottom.getRootView().getHeight());
                Log.v(TAG, "detailMainRL.getHeight() = " + includeBottom.getHeight());
                int heightDiff = includeBottom.getRootView().getHeight() - includeBottom.getHeight(); //被挤压的view高度
                Log.e(TAG, "onGlobalLayout: " + heightDiff);

                if (heightDiff < 800) { // 说明键盘是弹出状态
                    Log.v(TAG, "键盘弹出状态");
                    showInput();
                } else {
                    Log.v(TAG, "键盘收起状态");
                    hideInput();
                }
            }
        });
    }
}